<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>菜单管理</title>
    <link rel="stylesheet" href="__PUBLIC__/easyweb/assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="__PUBLIC__/easyweb/assets/module/admin.css?v=318"/>
	<style>
		@media screen and (min-width: 992px){
			#authoritiesEditForm .layui-col-md6{width: unset;float: none}
		}
	</style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="authoritiesTable"></table>
        </div>
    </div>
</div>
<script type="text/html" id="authoritiesTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="authoritiesEditDialog">
    <form id="authoritiesEditForm" lay-filter="authoritiesEditForm" class="layui-form model-form"
          style="padding-right: 20px;">
        <input name="authorityId" type="hidden"/>
        <div class="layui-row">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">上级菜单</label>
                    <div class="layui-input-block">
                        <div id="authoritiesEditParentSel" class="ew-xmselect-tree"></div>
                    </div>
                </div>
                
				<div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">名称:</label>
                    <div class="layui-input-block">
                        <input name="authorityName" placeholder="请输入名称" class="layui-input"
                               lay-verType="tips" lay-verify="required" required/>
                    </div>
                </div>
				<div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">排序号:</label>
                    <div class="layui-input-block">
                        <input name="order_num" placeholder="请输入排序号" type="number" class="layui-input"
                               lay-verType="tips" lay-verify="required|number" required/>
                    </div>
                </div>
				<div class="layui-form-item" id="menu_type_box">
                    <label class="layui-form-label layui-form-required">菜单类型:</label>
                    <div class="layui-input-block">
						<input name="menu_type" type="radio" lay-filter="raQT" value="M" title="目录" checked/>
                        <input name="menu_type" type="radio" lay-filter="raQT" value="C" title="菜单"/>
                        <input name="menu_type" type="radio" lay-filter="raQT" value="F" title="按钮"/>
                    </div>
                </div>
            </div>
            <div class="layui-col-md6" id="qaRIGroup"></div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="authoritiesEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>
<script type="text/javascript" src="__PUBLIC__/easyweb/assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="__PUBLIC__/easyweb/assets/js/common.js?v=318"></script>
<script type="text/html" id="raQTmc">
    <div class="layui-form-item">
		<label class="layui-form-label">url:</label>
		<div class="layui-input-block">
			<input name="url" placeholder="请输入url" class="layui-input"/>
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">图标:</label>
		<div class="layui-input-block">
			<input type ="hidden" name="icon" placeholder="请输入图标" class="layui-input" id="icon_menu"/>
			<input type="text" id="iconPicker3-2" lay-filter="iconPicker3-2" class="layui-input">
		</div>
	</div>
</script>
<script type="text/html" id="raQTf">
	<div class="layui-form-item">
		<label class="layui-form-label layui-form-required">权限标识</label>
		<div class="layui-input-block">
		  <input type="radio" name="perms" lay-verify="perms" value="1" title="查看">
		  <input type="radio" name="perms" lay-verify="perms" value="2" title="添加">
		  <input type="radio" name="perms" lay-verify="perms" value="3" title="修改">
		  <input type="radio" name="perms" lay-verify="perms" value="4" title="删除">
		</div>
	</div>
</script>
<script>
    layui.use(['layer', 'form', 'admin', 'treeTable', 'util', 'xmSelect','iconPicker'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var treeTable = layui.treeTable;
        var util = layui.util;
        var xmSelect = layui.xmSelect;
        var tbDataList = [];
		var iconPicker = layui.iconPicker;
		
		function iconXuanze(){
			iconPicker.render({
                elem: '#iconPicker3-2',
                type: 'fontClass',
                search: true,
                page: true,
                limit: 12,
                click: function (data) {
					$('#icon_menu').val(data.icon);
                },
                success: function(d) {
                }
            });
			iconPicker.checkIcon('iconPicker3-2', 'layui-icon-cellphone');	
			$('#icon_menu').val('layui-icon-cellphone');
		}
        var insTb = treeTable.render({
            elem: '#authoritiesTable',
			url: '{:U("Sys/p_list")}',
			where: {type:'menu'},
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                '</p>'].join(''),
            tree: {
                iconIndex: 2,
                idName: 'authorityId',
                pidName: 'parentId',
                isPidData: true
            },
            cols: [[
                {type: 'checkbox'},
                {type: 'numbers'},
                {field: 'authorityName', title: '菜单名称', minWidth: 180},
				{field: 'authorityId', title: '主键id'},
				{field: 'level', title: '级别'},
                {title: '菜单图标', templet: '<p><i class="layui-icon {{d.icon}}"></i></p>', align: 'center', width: 100},
                {field: 'url', title: '菜单url'},
                {field: 'order_num', title: '排序号', align: 'center', width: 80},
                {
                    field: 'menu_type',title: '类型', templet: function (d) {
						var menu_type = null;
						var menu_class = null;
						if(d.menu_type == "M"){
							menu_type = "目录";
							menu_class = "layui-badge-green";
						}
						if(d.menu_type == "C"){
							menu_type = "菜单";
							menu_class = "layui-badge-red";
						}
						if(d.menu_type == "F"){
							menu_type = "按钮";
							menu_class = "layui-badge-gray";
						}
                        return '<span class="layui-badge '+menu_class+'">'+menu_type+'</span>';
                    }, align: 'center', width: 80
                },
                {title: '创建时间', templet: '<p>{{layui.util.toDateString(d.createTime)}}</p>', align: 'center', minWidth: 150},
                {title: '操作', toolbar: '#authoritiesTbBar', align: 'center', width: 120}
            ]],
        });
        treeTable.on('tool(authoritiesTable)', function (obj) {
            if (obj.event === 'edit') { 
                showEditModel(obj.data);
				$("#menu_type_box").hide();
            } else if (obj.event === 'del') { 
                doDel(obj);
            }
        });
        treeTable.on('toolbar(authoritiesTable)', function (obj) {
            if (obj.event === 'add') {
                showEditModel();
            } else if (obj.event === 'del') { 
                var checkRows = insTb.checkStatus();
                if (checkRows.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.map(function (d) {
                    return d.authorityId;
                });
				var level = null;
				if( checkRows.length == 2){
					level = checkRows[1].level;
				}else{
					level = checkRows[0].level;
				}
                doDel({ids: ids,level: level});
            }
        });
		function changeQT(value,type) {
            if (value == "F") {
                $('#qaRIGroup').html($('#raQTf').html());
				form.render('radio');
                form.render('select', 'authoritiesEditForm');
				form.verify({
					perms: function(value){
					  if( typeof($('input[name="perms"]:checked').val()) == "undefined"){
						return '权限标识必选';
					  }
					}
				});
            } else {
                $('#qaRIGroup').html($('#raQTmc').html());
				iconXuanze();
                form.render('select', 'authoritiesEditForm');
            }
        }
        function showEditModel(mData) {
            admin.open({
                type: 1,
                area: '600px',
                title: (mData ? '修改' : '添加') + '权限',
                content: $('#authoritiesEditDialog').html(),
                success: function (layero, dIndex) {
				changeQT('M');
				form.on('radio(raQT)', function (data) {
					changeQT(data.value);
				});
				if (mData && mData.menu_type) {
					if(mData.menu_type == "F"){
						changeQT('F');
					}else{
						if(mData.icon != "" || mData.icon == null ){
							iconPicker.checkIcon('iconPicker3-2', mData.icon);
						}else{
							iconPicker.checkIcon('iconPicker3-2', '');
						}
					}
				}
                    form.val('authoritiesEditForm', mData);
                    form.on('submit(authoritiesEditSubmit)', function (data) {
                        data.field.parentId = insXmSel.getValue('valueStr');
						if( insXmSel.getValue().length == 0 ){
							data.field.f_level = 0;
						}else{
							data.field.f_level = insXmSel.getValue()[0].level;
						}
                        var loadIndex = layer.load(2);
						$.post(mData ? '{:U("Sys/p_Edit")}' : '{:U("Sys/p_Add")}', {data: data.field,type:'menu'}, function (res) {
                            layer.close(loadIndex);
                            if (res.code === 200) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.refresh();
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });
                    var insXmSel = xmSelect.render({
                        el: '#authoritiesEditParentSel',
                        height: '250px',
                        data: insTb.options.data,
                        initValue: mData ? [mData.parentId] : [],
                        model: {label: {type: 'text'}},
                        prop: {
                            name: 'authorityName',
                            value: 'authorityId'
                        },
                        radio: true,
                        clickClose: true,
                        tree: {
                            show: true,
                            indent: 15,
                            strict: false,
                            expandedKeys: []
                        }
                    });
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
        }
        function doDel(obj) {
		if(obj.hasOwnProperty('data')){
			if(obj.data.hasOwnProperty('children')){
				layer.msg("当前分类存在未删除下级，请先删除下级或使用选框批量删除", {icon: 2});
				error;
			}
		}
		var f_level = null;
		if(obj.data){
			f_level = obj.data.level; 
		}else{
			f_level = obj.level;
		}
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
				$.post('{:U("Sys/p_Delete")}', {id: obj.data ? obj.data.authorityId : obj.ids,f_level:f_level,type:'menu'}, function (res) {
                    layer.close(loadIndex);
                    if (res.code === 200) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.refresh();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }

    });
</script>
</body>
</html>